<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      style="text-align: left"
    >
      <el-form-item label="事故类型">
        <el-select v-model="formInline.recordTypeId" placeholder="事故类型">
          <el-option label="请选择" value=""></el-option>
          <el-option label="工程事故" value="1"></el-option>
          <el-option label="安全事故" value="2"></el-option>
          <el-option label="消防事故" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          style="background-color: green; color: white"
          icon="el-icon-search"
          @click="list"
          >查询</el-button
        >
        <el-button style="color: gray" icon="el-icon-refresh">重置</el-button>
        <el-button style="color: green" icon="el-icon-plus" @click="add"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :row-class-name="tableRowClassName"
      >
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="number" label="事件编号" align="center">
        </el-table-column>
        <el-table-column prop="recordTypeId" label="事故类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.recordTypeId == 1">工程事故</span>
            <span v-if="scope.row.recordTypeId == 2">安全事故</span>
            <span v-if="scope.row.recordTypeId == 3">消防事故</span>
          </template>
        </el-table-column>
        <el-table-column prop="scene" label="发生地点" align="center">
        </el-table-column>
        <el-table-column prop="datetime" label="发生时间" align="center">
        </el-table-column>
        <el-table-column prop="person" label="责任人" align="center">
        </el-table-column>
        <el-table-column label="事故详情" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              style="color: green"
              @click="examine(scope.row)"
              >点击查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" style="color: orange" @click="upd(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" style="color: red" @click="del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="countnum"
    >
    </el-pagination>

    <el-dialog width="600px" title="详情信息" :visible.sync="dialogFormVisible">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="180px"
        style="width: 600px; margin: 2px auto"
      >
        <el-form-item label="事件编号" prop="equipmentCoding">
          {{ form.number }}
        </el-form-item>
        <el-form-item label="事故类型" prop="originalNum">
          {{ form.recordTypeId }}
          <template slot-scope="scope">
            <span v-if="scope.row.recordTypeId == 1">工程事故</span>
            <span v-if="scope.row.recordTypeId == 2">安全事故</span>
            <span v-if="scope.row.recordTypeId == 3">消防事故</span>
          </template>
        </el-form-item>
        <el-form-item label="发生地点" prop="equipmentName">
          {{ form.scene }}
        </el-form-item>
        <el-form-item label="发生时间" prop="equipmentLocaId">
          {{ form.datetime }}
        </el-form-item>
        <el-form-item label="责任人" prop="deviceType">
          {{ form.person }}
        </el-form-item>
        <el-form-item label="事故照片" prop="equipmentBrand">
          <el-image :src="src" style="width: 30px; height: 30px"></el-image>
        </el-form-item>
        <el-form-item label="事故描述" prop="equipmentModel">
          {{ form.accident }}
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible1">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        style="width: 500px; margin: 2px auto"
      >
        <el-form-item label="事故编号" prop="number">
          <el-input v-model="form.number"></el-input>
        </el-form-item>
        <el-form-item label="事故类型" prop="recordTypeId">
          <el-select
            v-model="form.recordTypeId"
            placeholder="请选择事故类型"
            style="width: 419px"
          >
            <el-option label="工程事故" value="1"></el-option>
            <el-option label="安全事故" value="2"></el-option>
            <el-option label="消防事故" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发生地点" prop="scene">
          <el-input v-model="form.scene"></el-input>
        </el-form-item>
        <el-form-item label="发生时间" prop="datetime">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.datetime"
              style="width: 419px"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="责任人" prop="person">
          <el-input v-model="form.person"></el-input>
        </el-form-item>
        <el-form-item label="事故图片" prop="picture">
          <el-upload
            style="width: 100px"
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="src" :src="src" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="事故详情" prop="accident">
          <el-input type="textarea" v-model="form.accident"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false" style="color: green"
          >取 消</el-button
        >
        <el-button @click="baocun" style="color: green">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  GetRecord,
  GetTotaocount,
  updateRecord,
  deleteRecord,
} from "@/api/safeManage/Record";
export default {
  data() {
    return {
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 0) {
          return "warning-row";
        } else if (rowIndex === 2) {
          return "success-row";
        }
        return "";
      },
      src: "https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg",
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      ids: {},
      form: {
        number: "",
        recordTypeId: "",
        scene: "",
        datetime: "",
        person: "",
        picture: "1",
        accident: "",
      },
      formInline: {
        pageIndex: 1,
        pageSize: 2,
        recordTypeId: "",
      },
      countnum: 0,
      count: 0,
    };
  },
  created() {
    this.list();
    this.totalcount();
  },
  methods: {
    //显示
    list() {
      GetRecord(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.formInline.pageIndex = val;
      this.list();
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    //删除
    del(row) {
      this.ids = row;
      deleteRecord(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.list();
        } else {
          this.$message.error("删除失败");
          return;
        }
      });
    },
    //查看
    examine(row) {
      this.dialogFormVisible = true;
      this.form = row;
    },
    //修改
    upd(row) {
      this.dialogFormVisible1 = true;
      this.form = row;
    },
    baocun() {
      updateRecord(this.form).then((res) => {
        if (res.data) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.list();
          this.dialogFormVisible1 = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //跳转
    add() {
      this.$router.push("/SafeManage/AddRecord");
    },
  },
};
</script>
<style>
</style>